import { request, encapsulation } from './request' // encapsulation
import { getToken } from '@/network/bookManager.js'

/*
 *@functionName: 获取 菜单列表
 *@params1: 参数1
 *@description:
 *@author: chendi
 *@date: 2020-09-04 23:48:50
 *@version: V1.0.5
 */
export async function queryMenuList(option) {
  let token = await getToken()
  return request({
    url: `/api/tcb/invokecloudfunction?&access_token=${token.access_token}&env=zhuma-mbnti&name=queryMenuList`,
    method: `post`,
    data: JSON.stringify(option)
  })
}
/*
 *@functionName:  获取 菜单列表总数
 *@params1: 参数1
 *@description:
 *@author: chendi
 *@date: 2020-09-05 20:21:54
 *@version: V1.0.5
 */
export async function queryMenuListTotal(option) {
  let token = await getToken()
  return request({
    url: `/api/tcb/invokecloudfunction?&access_token=${token.access_token}&env=zhuma-mbnti&name=queryMenuListTotal`,
    method: `post`,
    data: JSON.stringify(option)
  })
}
/*
 *@functionName:  添加菜单
 *@params1: 参数1
 *@description:
 *@author: chendi
 *@date: 2020-09-05 14:07:13
 *@version: V1.0.5
 */
export function addMenu(option) {
  return encapsulation({
    url: '/api/tcb/databaseadd',
    method: 'post',
    data: {
      query: `db.collection('boxAccess')
         .add({
           data: [{
            menuIcon: '${option.menuIcon}',
            menuName: '${option.menuName}',
            menuUrl: '${option.menuUrl}',
            parentId:'${option.parentId}'
             }]
         })`
    }
  })
}
/*
 *@functionName: 删除菜单
 *@params1: 参数1
 *@description:
 *@author: chendi
 *@date: 2020-09-05 14:22:55
 *@version: V1.0.5
 */
export function removeMenuById(option) {
  return encapsulation({
    url: '/api/tcb/databasedelete',
    method: 'post',
    data: {
      query: `db.collection('boxAccess')
      .where({
        _id:'${option}'
      }).remove()`
    }
  })
}
/*
 *@functionName: 修改 菜单
 *@params1: 参数1
 *@description:
 *@author: chendi
 *@date: 2020-09-05 17:18:10
 *@version: V1.0.5
 */
export function updateMenuById(option) {
  return encapsulation({
    url: '/api/tcb/databaseupdate',
    method: 'post',
    data: {
      query: `db.collection("boxAccess")
      .where({
        _id:'${option._id}'
      }).update({
        data:{
          ${option.menuIcon ? `menuIcon:'${option.menuIcon}',` : ``}
          ${option.menuName ? `menuName:'${option.menuName}',` : ``}
          ${option.menuUrl ? `menuUrl:'${option.menuUrl}',` : ``}
          ${option.parentId ? `parentId:'${option.parentId}',` : ``}
        }
      })`
    }
  })
}
/*
 *@functionName: 查询所有角色
 *@params1: 参数1
 *@description:
 *@author: chendi
 *@date: 2020-09-06 12:06:45
 *@version: V1.0.5
 */
export function queryRole(option) {
  return encapsulation({
    url: '/api/tcb/databasequery',
    method: 'post',
    data: {
      query: `db.collection("boxRole")
      .where({
        ${
          option.roleName
            ? `roleName:db.RegExp({
          regexp: '${option.roleName}',
          options: 'i',
        })`
            : ``
        }
        ${option.roleStatus&&option.roleStatus != undefined ? `roleStatus:${option.roleStatus}` : ``}
      }).skip(${option.size * (option.page - 1)}).limit(${option.size})
      .get()`
    }
  })
}
/*
 *@functionName: 角色总数
 *@params1: 参数1
 *@description:
 *@author: chendi
 *@date: 2020-09-07 01:34:15
 *@version: V1.0.5
*/
export function queryRoleTotal(option) {
  return encapsulation({
    url: '/api/tcb/databasecount',
    method: 'post',
    data: {
      query: `db.collection("boxRole")
      .where({
        ${
          option.roleName
            ? `roleName:db.RegExp({
          regexp: '${option.roleName}',
          options: 'i',
        })`
            : ``
        }
        ${option.roleStatus&&option.roleStatus != undefined  ? `roleStatus:${option.roleStatus}` : ``}
      }).count()`
    }
  })
}
/*
 *@functionName: 添加角色
 *@params1: 参数1
 *@description:
 *@author: chendi
 *@date: 2020-09-06 12:59:39
 *@version: V1.0.5
 */
export function addRole(option) {
  return encapsulation({
    url: '/api/tcb/databaseadd',
    method: 'post',
    data: {
      query: `db.collection('boxRole')
         .add({
           data: [{
            roleName: '${option.roleName}',
            roleStatus: ${option.roleStatus}
             }]
         })`
    }
  })
}
/*
 *@functionName: 根据id 修改 角色信息
 *@params1: 参数1
 *@description:
 *@author: chendi
 *@date: 2020-09-06 13:01:16
 *@version: V1.0.5
 */
export function updateRoleById(option) {
  return encapsulation({
    url: '/api/tcb/databaseupdate',
    method: 'post',
    data: {
      query: `db.collection("boxRole")
      .where({
        _id:'${option._id}'
      }).update({
        data:{
          ${option.roleName ? `roleName:'${option.roleName}',` : ``}
          ${
            option.roleStatus != undefined
              ? `roleStatus:${option.roleStatus},`
              : ``
          }
        }
      })`
    }
  })
}
/*
 *@functionName: 根据id 删除 角色
 *@params1: 参数1
 *@description:
 *@author: chendi
 *@date: 2020-09-06 13:03:56
 *@version: V1.0.5
*/
export function removeRoleById(option) {
  return encapsulation({
    url: '/api/tcb/databasedelete',
    method: 'post',
    data: {
      query: `db.collection('boxRole')
      .where({
        _id:'${option}'
      }).remove()`
    }
  })
}
/*
 *@functionName: 根据 角色id 删除 角色 与 菜单关联
 *@params1: 参数1
 *@description:
 *@author: chendi
 *@date: 2020-09-06 22:56:05
 *@version: V1.0.5
*/
export function removeRoleMenuById(option) {
  return encapsulation({
    url: '/api/tcb/databasedelete',
    method: 'post',
    data: {
      query: `db.collection('boxRoleAccess')
      .where({
        roleId:'${option}'
      }).remove()`
    }
  })
}
/*
 *@functionName: 添加 角色 与 菜单关联
 *@params1: 参数1
 *@description:
 *@author: chendi
 *@date: 2020-09-06 22:58:47
 *@version: V1.0.5
*/
export function addRoleMenu(option) {
  return encapsulation({
    url: '/api/tcb/databaseadd',
    method: 'post',
    data: {
      query: `db.collection('boxRoleAccess')
         .add({
           data: ${option}
         })`
    }
  })
}
/*
 *@functionName: 查询当前角色下的 菜单
 *@params1: 参数1
 *@description:
 *@author: chendi
 *@date: 2020-09-06 23:18:38
 *@version: V1.0.5
*/
export function queryRoleMenu(option) {
  return encapsulation({
    url: '/api/tcb/databasequery',
    method: 'post',
    data: {
      query: `db.collection("boxRoleAccess")
      .where({
       roleId:'${option}'
      })
      .get()`
    }
  })
}